unit u_animsprite;

interface

uses
  sysutils,
  system.Drawing,
  Microsoft.DirectX.Direct3DX,
  Microsoft.DirectX.Direct3D,
  Microsoft.DirectX;

type

  c_animsprite = class
    private
      s_sprite : sprite;
      //la texture du sprite

      s_texture : Texture;
      s_texture_sel : Texture;

      //ou il doit aller en pourcentage de la fenetre
      s_x, s_y : single;
      //le pas ou la vitesse si on veut bitch!
      s_pas : integer;
      pas_x, pas_y: single;
      //la résolution de la fenetre
      res_x, res_y : integer;
      //La position courante
      pos_x, pos_y : single;
      //Ou il doit aller
      arr_x, arr_y : single;
      //indique s'il est en mouvement
      is_moving : Boolean;
      //Si on est en pause
      is_paused : boolean;
    public

      constructor create(dev : Device; f_name1, f_name2 : string; posy, posx : Single);
      procedure change_resolution(t_x, t_y, _newx, _newy : integer);
      procedure faire_venir;
      procedure faire_sortir;
      function est_dedans: boolean;
      function est_dehors : boolean;
      procedure render;
      procedure dispose;
      procedure pause;
      procedure play;
      procedure set_out;
      procedure set_in;
      procedure set_pas(_n: integer);


  end;

implementation

  procedure c_animsprite.set_pas(_n: Integer);
  begin
    s_pas := _n;
  end;


  constructor c_animsprite.create(dev: Device; f_name1, f_name2: string; posy, posx: Single);
    begin
      inherited create;
      s_texture_sel := TextureLoader.FromFile(dev, f_name2);
      s_texture := Textureloader.FromFile(dev, f_name1);
      s_y := posy;
      s_x := posx;
      pos_x := -400;
      pos_y := -400;
      is_moving := False;

      is_paused := true;
      s_sprite := sprite.Create(dev);
      s_pas := 300;

    end;

  procedure c_animsprite.change_resolution(t_x: Integer; t_y: Integer; _newx, _newy : integer);
    begin
      _newx := Trunc(_newx);
      _newy := Trunc(_newy);
      res_x := t_x;
      res_y := t_y;
      s_x := _newx;
      s_y := _newy;
      if est_dehors then
        begin
          if pos_x <> - 400 then
            pos_x := res_x + 400;
          if pos_y <> - 400 then
            pos_y := res_y + 400;
        end;
//      arr_x := t_x;
//      arr_y := t_y;
      if not is_moving and est_dedans then
        begin
          arr_x := _newx;
          pos_x := _newx;
          arr_y := _newy;
          pos_y := _newy;
        end;
    end;

  procedure c_animsprite.faire_venir;
  var
    anim_rnd : integer;
    begin
      //1 du haut gauche
      //2 du haut droit
      //3 du bas gauche
      //4 du bas droit


      is_paused := true;

      arr_x := s_x;
      arr_y := s_y;

      is_moving := True;


      anim_rnd := Random(4) + 1;
      case anim_rnd of
        1 : begin
              pos_x := - 400;
              pos_y := - 400;
              pas_x := (arr_x + 400) / s_pas;
              pas_y := (arr_y + 400) / s_pas;
        end;
        2 : begin
              pos_x := res_x + 400;
              pos_y := - 400;
              pas_x := - (res_x - arr_x + 400) / s_pas;
              pas_y := (arr_y + 400) / s_pas;
        end;
        3 : begin
              pos_x := - 400;
              pos_y := res_y + 400;
              pas_x := (arr_x + 400) / s_pas;
              pas_y := - (res_y - arr_y + 400) / s_pas;
        end;
        4 : begin
              pos_x := res_x + 400;
              pos_y := res_y + 400;
              pas_x := - (res_x - arr_x + 400) / s_pas;
              pas_y := - (res_y - arr_y + 400) / s_pas;
        end;
      end;
    end;

  procedure c_animsprite.faire_sortir;
  var
    anim_rnd : integer;
    begin
      //1 du haut gauche
      //2 du haut droit
      //3 du bas gauche
      //4 du bas droit

      is_moving := True;
      is_paused := true;

      anim_rnd := Random(4) + 1;
      case anim_rnd of
        1 : begin
              arr_x := - 400;
              arr_y := - 400;
              pas_x := - (pos_x + 400) / s_pas;
              pas_y := - (pos_y + 400) / s_pas;
        end;
        2 : begin
              arr_x := res_x + 400;
              arr_y := - 400;
              pas_x := (pos_x + 400) / s_pas;
              pas_y := - (pos_y + 400) / s_pas;
        end;
        3 : begin
              arr_x := - 400;
              arr_y := res_y + 400;
              pas_x := - (pos_x + 400) / s_pas;
              pas_y := (pos_y + 400) / s_pas;
        end;
        4 : begin
              arr_x := res_x + 400;
              arr_y := res_y + 400;
              pas_x := (pos_x + 400) / s_pas;
              pas_y := (pos_y + 400) / s_pas;
        end;
      end;
    end;



  procedure c_animsprite.render;
    begin
      if is_moving then
        begin
          pos_x := pos_x + pas_x;
          pos_y := pos_y + pas_y;
          if abs(trunc(pos_x - arr_x)) in [0..3] then
            begin
              pos_x := trunc(arr_x);
              pos_y := trunc(arr_y);
              is_moving := false;
              is_paused := false;
            end;
          if abs(Trunc(pos_y - arr_y)) in [0..3] then
            begin
              pos_y := trunc(arr_y);
              pos_x := trunc(arr_x);
              is_moving := false;
              is_paused := false;
            end;
        end
        else
        if est_dedans and not is_paused then
          begin
            pos_x := pos_x + (Random(10) - 5) / 5;
            pos_y := pos_y + (Random(10) - 5) / 5;
            if pos_x - arr_x > 10 then
              pos_x := pos_x - 1;
            if pos_x - arr_x < - 10 then
              pos_x := pos_x + 1;
            if pos_y - arr_y > 10 then
              pos_y := pos_y - 1;
            if pos_y - arr_y < - 10 then
              pos_y := pos_y + 1;            
          end;

      s_sprite.&Begin(spriteflags.AlphaBlend);
      if is_paused then
        s_sprite.Draw(s_texture_sel, Vector3.Create(0, 0, 0), Vector3.Create(pos_x, pos_y, 0), Color.White.ToArgb)
      else
        s_sprite.Draw(s_texture, Vector3.Create(0, 0, 0), Vector3.Create(pos_x, pos_y, 0), Color.White.ToArgb);
      s_sprite.&End;
    end;

  procedure c_animsprite.dispose;
    begin
      s_sprite.Dispose;
      s_texture.Dispose;
    end;

  procedure c_animsprite.pause;
    begin
      is_paused := True;
    end;

  procedure c_animsprite.play;
    begin
      is_paused := false;
    end;

  function c_animsprite.est_dedans;
    begin
      Result := (is_moving = false) and (pos_x <> -400) and (pos_x <> res_x + 400);
    end;

  function c_animsprite.est_dehors;
    begin
      Result := (not is_moving) and ((pos_x = -400) or (pos_x = res_x + 400));
    end;

  procedure c_animsprite.set_out;
    begin
      is_moving := false;
      pos_x := - 400;
      pos_y := - 400;
    end;

  procedure c_animsprite.set_in;
    begin
      is_moving := false;
      pos_x := s_x;
      pos_y := s_y;
    end;


end.
